<?php

class Admin_OpenId_ServerAction extends BlogAdminBaseAction
{
    public function execute(AgaviRequestDataHolder $rd)
    {
        /*
        $x = @file_get_contents('/tmp/requests.txt');
        $x .= "_POST\n";
        $x .= var_export($GLOBALS['HTTP_POST_VARS'], true);
        $x .= "_GET\n";
        $x .= var_export($GLOBALS['HTTP_GET_VARS'], true);
        $x .= "\n-----------\n";
        @file_put_contents('/tmp/requests.txt', $x);
        */

        $user = $this->getContext()->getUser();
        require_once 'Auth/OpenID/Server.php';
        $params = $rd->getParameters();
        
        // $params['openid_mode'] === 'associate'
        // $params['openid_mode'] === 'checkid_setup'
        if (($params['openid_mode'] === 'checkid_setup' || $params['openid_mode'] === 'checkid_immediate')
            && !$user->hasRole('blog-owner')) 
        {
            // redirect to 404 error
            return array('Public', 'Error404Success');
        }
        
        $query = $params;
        /*
        foreach ($params as $k=>$p) {
            if (strpos($k, 'openid_') == 0) {
                $query['openid.'.substr($k, 7)] = $p;
            }
        }
		*/
        $retranslate_keys = array('mode', 'return_to', 'identity', 'trust_root', 'assoc_type',
            'dh_consumer_public', 'assoc_handle', 'sig', 'signed', 'response_nonce');
        foreach ($retranslate_keys as $k) {
            $fk = 'openid_'.$k;
            if (isset($params[$fk])) {
                $query['openid.'.$k] = $params[$fk];
            }
        }
        
        $server = $this->getServer();
        $request = $server->decodeRequest($query);
        
        if (!$request instanceof Auth_OpenID_AssociateRequest 
            && !$request instanceof Auth_OpenID_CheckIDRequest
            && !$request instanceof Auth_OpenID_CheckAuthRequest) 
        {
            return 'ErrorWrongRequest';
        }
        
        $this->setAttribute('openid_server', $server);
        $this->setAttribute('openid_request', $request);
        
        if ($params['openid_mode'] === 'checkid_setup' || $params['openid_mode'] === 'checkid_immediate') {
            // display confirmation view
            return 'Confirm';
        } elseif ($params['openid_mode'] === 'associate' || $params['openid_mode'] === 'check_authentication') {
            // display response to remote service
            return 'WebResponse';
        } else {
            return array('Public', 'Error404Success');
        }
        
        //$response = $server->handleRequest($request);
        //$web_response = $server->encodeResponse($response);
        
        //var_dump($params, $web_response);
    }
    
	/**
	 * Returns the default view if the action does not serve the request
	 * method used.
	 *
	 * @return     mixed <ul>
	 *                     <li>A string containing the view name associated
	 *                     with this action; or</li>
	 *                     <li>An array with two indices: the parent module
	 *                     of the view to be executed and the view to be
	 *                     executed.</li>
	 *                   </ul>
	 */
	public function getDefaultViewName()
	{
		return 'Success';
	}

	public function isSecure()
    {
        return false;
    }
    
    protected function getServer()
    {
        require_once 'Auth/OpenID/Server.php';
        require_once 'Auth/OpenID/FileStore.php';
        
    
	    $store_dir = AgaviConfig::get('wereword.openid_store_dir');
	    if (is_null($store_dir)) {
	        throw new WereWordException('wereword.openid_store_dir config option is not set!');
	    }
	    
	    if (!isset($this->store)) {
    	    if (!file_exists($store_dir) && !mkdir($store_dir)) {
    	        return null;
    	    }
	        $this->store = new Auth_OpenID_FileStore($store_dir);
	    }

	    if (!isset($this->server)) {
            $ro = $this->getContext()->getRouting();
	        $server_url = $ro->gen('openid.confirm', array(), array('relative'=>false));
	        $this->server = new Auth_OpenID_Server($this->store, $server_url);
	    }
	    return $this->server;
    }
}

?>
